#include <REGX52.H>
#include "I2C.h"
#include "Delay.h"

//地址+写操作：1010(AT24C02固定地址) 000(E0 E1 E2端口) 0(0:写 1：读)
#define AT24C02_ADDRESSW  	0xA0
#define AT24C02_ADDRESSR  	0xA1

/**
  * @brief  AT24C02写入一个字节
  * @param  WordAddress 要写入字节的地址  0~255
	* @param 	Data 要写入的字节数据
  * @retval 无
  */
void AT24C02_WriteByte(unsigned char WordAddress,Data)
{
	I2C_Start();	//开始
	I2C_SendByte(AT24C02_ADDRESSW);	//地址
	I2C_ReceiveAck();	//接收应答 此时返回值确为0
	I2C_SendByte(WordAddress);	//写字地址
	I2C_ReceiveAck();	//接收应答	
	I2C_SendByte(Data);		//写数据
	I2C_ReceiveAck();	//接收应答	
	I2C_Stop();	//结束
	Delay(5);//写入数据后需要有个5ms的时间延迟
}

/**
  * @brief  AT24C02读取一个字节
  * @param  WordAddress 要读取字节的地址  0~255
  * @retval 要读出的数据
  */
unsigned char AT24C02_ReadByte(unsigned char WordAddress)
{
	unsigned char Data;
	I2C_Start();	//开始	
	I2C_SendByte(AT24C02_ADDRESSW);	//地址	
	I2C_ReceiveAck();	//接收应答
	I2C_SendByte(WordAddress);	//写字地址
	I2C_ReceiveAck();	//接收应答
	
	I2C_Start();	//开始		
	I2C_SendByte(AT24C02_ADDRESSR);	//地址
	I2C_ReceiveAck();	//接收应答	
	Data=I2C_ReceiveByte();	//接收数据
	I2C_SendAck(1);	//发送应答  1：不应答
	I2C_Stop();	//结束	
	return Data;
}